package com.etcbase.dcs.utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.Window;

/**
 * This is a fragment that will be used during transition from activities to
 * fragments.
 */
public abstract class ActivityHostFragment extends LocalActivityManagerFragment {

	protected abstract Class<? extends Activity> getActivityClass();

	private final static String ACTIVITY_TAG = "ActivityHostFragment";

	@Override
	public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroupContainer,
			Bundle savedInstanceBundle) {
		Intent intent = new Intent(getActivity(), getActivityClass());

		final Window window = getLocalActivityManager().startActivity(ACTIVITY_TAG,
				intent);
		final View view = window != null ? window.getDecorView() : null;

		if (view != null) {
			ViewParent viewParent = view.getParent();
			if (viewParent != null) {
				ViewGroup viewGroup = (ViewGroup) viewParent;
				viewGroup.removeView(view);
			}

			view.setVisibility(View.VISIBLE);
			view.setFocusableInTouchMode(true);
			if (view instanceof ViewGroup) {
				((ViewGroup) view)
						.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
			}
		}
		return view;
	}
}
